package com.jinxin.platform.cdysylives.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdysylives.bean.CdysylivesSecretkey;
import com.jinxin.platform.cdysylives.bean.bo.OptionVo;
import com.jinxin.platform.cdysylives.common.Constant;
import com.jinxin.platform.cdysylives.mapper.CdysylivesSecretkeyMapper;
import com.jinxin.platform.cdysylives.rpc.CdysyliveRpc;
import com.jinxin.platform.cdysylives.service.ICdysylivesSecretkeyService;
import com.jinxin.platform.cdysylives.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @Author 那个男人
 * @Date 2023/7/24 17:33
 * @Version 1.0
 */
@Service
public class CdysylivesSecretkeyServiceImpl implements ICdysylivesSecretkeyService {
    @Autowired
    private CdysylivesSecretkeyMapper secretkeyMapper;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdysyliveRpc cdysyliveRpc;

    @Override
    public CdysylivesSecretkey selectOne(HashMap param) {
        return secretkeyMapper.selectOne(param);
    }

    @Override
    public List<CdysylivesSecretkey> list(HashMap param) {
        return secretkeyMapper.list(param);
    }

    @Override
    public List<CdysylivesSecretkey> listPage(HashMap param) {
        return secretkeyMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return secretkeyMapper.listPageCount(param);
    }

    @Override
    public JsonResult saveOrUpdate(CdysylivesSecretkey record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            HashMap param = new HashMap();
            param.put("communityId",record.getCommunityId());
            param.put("communityName",record.getCommunityName());
            CdysylivesSecretkey secretkey = secretkeyMapper.selectOne(param);
            if(secretkey != null){
                return JsonResult.error("该小区已存在秘钥");
            }
            record.setId(String.valueOf(idWorker.nextId()));
            record.setCreateTime(LocalDateTime.now());
            record.setIsEnabled(Constant.DISABLED);
            count = secretkeyMapper.insert(record);
            msg = "新增成功";
        }else {
            record.setUpdateTime(LocalDateTime.now());
            count = secretkeyMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("ID不存在");
        }
        int result = secretkeyMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult batchEnable(List<String> idList) {
        if(idList.size() == 0){
            return JsonResult.error("参数不存在");
        }
        int num = secretkeyMapper.batchEnable(idList);
        return num > 0 ? JsonResult.ok("启用成功",null) : JsonResult.error("启用失败");
    }

    @Override
    public JsonResult batchDisable(List<String> idList) {
        if(idList.size() == 0){
            return JsonResult.error("参数不存在");
        }
        int num = secretkeyMapper.batchDisable(idList);
        return num > 0 ? JsonResult.ok("禁用成功",null) : JsonResult.error("禁用失败");
    }

    @Override
    public JsonResult queryCommunityList() {
        Set<String> orgNameSet = cdysyliveRpc.getOrgNameSet();
        if(orgNameSet.isEmpty()){
            return JsonResult.error("没有下级组织");
        }
        return cdysyliveRpc.queryCommunityList(orgNameSet);
    }
}
